package com.project.controller;

import com.project.model.Reorder;
import com.project.model.Stu;
import com.project.model.Totalorder;
import com.project.model.User;
import com.project.service.GetStuidService;
import com.project.service.ReOrderService;
import com.sun.corba.se.impl.oa.toa.TOA;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/reorder")
public class ReOrderController {
	private ReOrderService reOrderService;
	private GetStuidService getStuidService;
	
	public ReOrderService getReOrderService() {
		return reOrderService;
	}
	@Autowired
	public void setReOrderService(ReOrderService reOrderService) {
		this.reOrderService = reOrderService;
	}
	public GetStuidService getGetStuidService() {
		return getStuidService;
	}
	@Autowired
	public void setGetStuidService(GetStuidService getStuidService) {
		this.getStuidService = getStuidService;
	}
	@RequestMapping("/selectreorder")
	public JSONObject selectReorder(HttpServletRequest request){
//		HttpSession session=request.getSession();
//		User user=(User)session.getAttribute("user");
		User user=new User();
		user.setUid(1);
		Stu stu=getStuidService.selectStuid(user);
		String stuid=null;
		stuid=stu.getStuid();
		
		Map<String,Object> map=new HashMap<String, Object>();
		JSONObject json =new JSONObject();
		List<String> list=this.getReOrderService().selectreorder(stuid);
		for (int i=0;i<list.size()&&list!=null;i++){
			Totalorder totalorder =this.getReOrderService().selectTotalorder(list.get(i));
			if(totalorder.getTtype().equals("1")){
				String tid=totalorder.getTid();
				String ttype="1";
				String uname=totalorder.getUser().getUname();
				String utel=totalorder.getUser().getUtel();
				String uidcard=totalorder.getUser().getUidcard();
				String myadd1=totalorder.getMyadd().getMyaddress();
				String myadd2=totalorder.getMyadd().getMyaddhousenum();
				String myadd3=totalorder.getMyadd().getMyaddroomnum();
				String name=totalorder.getAddr().getAname();
				String tel=totalorder.getAddr().getAtel();
				String add1=totalorder.getAddr().getAarea();
				String add2=totalorder.getAddr().getAaddress();
				String othername=totalorder.getOtheradd().getOname();
				String othertel=totalorder.getOtheradd().getOtel();
				String oaddress=totalorder.getOtheradd().getOaddress();
				Double fee=totalorder.getTfee();
				Double weight=totalorder.getTweight();
				String company=totalorder.getCompany().getCompanyname();
				map.put("订单编号",tid);
				map.put("订单类别",ttype);
				map.put("用户名",uname);
				map.put("用户电话",utel);
				map.put("身份证号",uidcard);
				map.put("用户地址",myadd1+myadd2+myadd3);
				map.put("寄件人姓名",name);
				map.put("寄件人电话",tel);
				map.put("寄件人地址",add1+add2);
				map.put("收件人姓名",othername);
				map.put("收件人电话",othertel);
				map.put("收件人地址",oaddress);
				map.put("小费",fee);
				map.put("重量",weight);
				map.put("快递公司",company);
			}else if (totalorder.getTtype().equals("2")){
				String tid=totalorder.getTid();
				String ttype="2";
				String uname=totalorder.getUser().getUname();
				String utel=totalorder.getUser().getUtel();
				String uidcard=totalorder.getUser().getUidcard();
				String myadd1=totalorder.getMyadd().getMyaddress();
				String myadd2=totalorder.getMyadd().getMyaddhousenum();
				String myadd3=totalorder.getMyadd().getMyaddroomnum();
				String code=totalorder.getTcode();
				Double fee=totalorder.getTfee();
				String company=totalorder.getCompany().getCompanyname();
				map.put("订单编号",tid);
				map.put("订单类别",ttype);
				map.put("用户名",uname);
				map.put("用户电话",utel);
				map.put("身份证号",uidcard);
				map.put("用户地址",myadd1+myadd2+myadd3);
				map.put("取货码",code);
				map.put("小费",fee);
				map.put("快递公司",company);
			}
			json.put(i+1,map);
		}
		return json;
	}
}
